<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
?>
<style type="text/css">
    .w100 {width: 85px; padding:5px 8px; border:1px solid #CCC; border-radius: 5px;}
    label {font-weight: normal;}
    .fixed-btn {position:fixed; right:35%;bottom:1%;}
    .price1 {width:60%;margin-bottom:5px;float:left;}
    .fa {font-size: 16px;}
</style>
<?=  yii\web\View::registerCssFile("/css/magic-check.css",['depends' => 'app\assets\AppAsset']) ?>
<div style="position:relative;">

<?php $form = ActiveForm::begin([
                                 'id' => 'template-room-form',
                                 'action' => '/product/product-room/save2',
                            ]); ?>
    <div class="product-room-form form-inline table-responsive">
        <h2 class="page-header"><small><?= $product->name ?> / <?= $product->go_time ?></small>
		 	<p class="pull-right">
            <a class="btn btn-danger" href="/product/product/view?id=<?=$product->id?>">返回详情</a>    
         	<!-- a class="btn btn-primary" href="/product/product-room/create?product_id=<?=$product->id?>&type=1">标准价格格式</a -->
			</p>
        </h2>
        <table class="table table-striped table-condensed">
            <tr>
                <th class="col-md-2">名称</th>
                <th class="col-md-1">第1/2人价格</th>
                <th class="col-md-1">第1/2人同业</th>
                <th class="col-md-1">第3/4人价格</th>
                <th class="col-md-1">第3/4人同业</th>
                <th class="col-md-1">单间差</th>
                <th class="col-md-1">后返</th>
                <th class="col-md-1">库存数</th>
                <th class="col-md-1">备注</th>
                <th class="col-md-1">售罄</th>
            </tr>
            <input type="hidden" id="product_id" name="product_id" value="<?= $product->id ?>" >

            <?php if($models){ ?>             
            <?php foreach($models as $key=>$model){ ?>             
            <tr class="item-base">
                <td class="col-md-2">
                    <input type="text" value="<?=$model['name']?>" name="ProductRoom[<?=$key?>][name]"  class="form-control">
                </td>
                <td>
                    <input type="text" value="<?=$model['price']?>" name="ProductRoom[<?=$key?>][price]" class="w100">
                </td>
                <td>
                  <input type="text" value="<?=$model['inside_price']?>" name="ProductRoom[<?=$key?>][inside_price]" class="w100">
                </td>
                <td>
                    <input type="text" value="<?=$model['price2']?>" name="ProductRoom[<?=$key?>][price2]" class="w100">
                </td>
                <td>
                <input type="text" value="<?=$model['inside_price2']?>" name="ProductRoom[<?=$key?>][inside_price2]" class="w100">
                </td>
                <td>
                    <input type="text" value="<?=$model['difference']?>" name="ProductRoom[<?=$key?>][difference]" class="w100">
                </td>

                <td>
            		<input type="text" id="room-commiss_price" class="w100 commiss-val" name="ProductRoom[<?= $key ?>][commiss_price]"  value="<?= isset($model['commiss_price']) ? $model['commiss_price'] : 0  ?>" placeholder="后返">
                </td>
                <td>
					<!-- ?= $product->getSurplusRoom($key); ? -->
                    <input type="text" value="<?=$model['num']?>" name="ProductRoom[<?=$key?>][num]" class="w100 stock" data-id=<?= $key ?> >
                </td>
                <td>
                    <input type="text" value="<?=$model['remark']?>" name="ProductRoom[<?=$key?>][remark]"  class="form-control">
                </td>
                <td>
                    <input class="magic-checkbox sell-out" <?= (isset($model['sell_out']) && $model['sell_out']==1) ? 'checked' : ''  ?> type="checkbox" id="productroom-sellout-<?=($key)?>" name="ProductRoom[<?=$key?>][sell_out]" data-id=<?=$key?> > 
                    <label for="productroom-sellout-<?= ($key)?>"></label>
                </td>
            </tr>
            <?php } ?>
            <?php }else{ ?>
             <tr class="item-base">
                <td class="col-md-2"><input type="text" name="ProductRoom[1][name]"  class="form-control"></td>
                <td><input type="text" name="ProductRoom[1][price]" class="w100"></td>
                <td><input type="text" name="ProductRoom[1][inside_price]" class="w100"></td>
                <td><input type="text" name="ProductRoom[1][price2]" class="w100"></td>
                <td><input type="text" name="ProductRoom[1][inside_price2]" class="w100"></td>
                <td><input type="text" name="ProductRoom[1][difference]" class="w100"></td>
            	<td><input type="text" id="room-commiss_price" class="w100 commiss-val" name="ProductRoom[1][commiss_price]"></td>
                <td><input type="text" name="ProductRoom[1][num]" class="w100"></td>
                <td><input type="text" name="ProductRoom[1][remark]"  class="form-control"></td>
                <td class="col-md-1">
                    <input class="magic-checkbox sell-out" type="checkbox" id="productroom-sellout-1" name="ProductRoom[1][sell_out]" data-id=1 > 
                    <label for="productroom-sellout-1"></label>
                </td>
            </tr>   
            <?php } ?>
        </table>
    
        <div><p class="text-right"><button type="button" class="btn btn-default add-item">添加</button></p></div>
    </div>
    <div class="form-group">
        <label class="label" style="color:red;">房间价格说明 :</label>
        <textarea name="Product[room_remark]" rows=4 class="form-control"><?= $product->room_remark ?></textarea> 
    </div>
    <hr>
    <?= Html::submitButton(Yii::t('app', '保 存'), ['class' => 'btn btn-primary col-md-3 fixed-btn']) ?>
<?php ActiveForm::end(); ?>
</div>

<?php app\components\JsBlock::begin() ?>
<script>
    $(function(){
        $(".add-item").click(function(){
            var num = $("tr").size();
            //var num = num-1;
            console.log(num);
            var html = '<tr>';
                html += '<td class="col-md-2"><input type="text" name="ProductRoom['+num+'][name]"  class="form-control"></td>';
                html += '<td><input type="text" name="ProductRoom['+num+'][price]" class="w100"></td>';
                html += '<td><input type="text" name="ProductRoom['+num+'][inside_price]" class="w100"></td>';
                html += '<td><input type="text" name="ProductRoom['+num+'][price2]" class="w100"></td>';
                html += '<td><input type="text" name="ProductRoom['+num+'][inside_price2]" class="w100"></td>';
                html += '<td><input type="text" name="ProductRoom['+num+'][difference]" class="w100"></td>';
                html += '<td><input type="text" name="ProductRoom['+num+'][commiss_price]" class="w100" placeholder="后返"></td>';
                html += '<td><input type="text" name="ProductRoom['+num+'][num]" class="w100"></td>';
                html += '<td><input type="text" name="ProductRoom['+num+'][remark]"  class="form-control"></td>';
                html += '<td><input class="magic-checkbox sell-out" type="checkbox" id="productroom-sellout-'+num+'" name="ProductRoom['+num+'][sell_out]" data-id='+num+'><label for="productroom-sellout-'+num+'"></label></td>';
                html += '</tr>';
            $(".table").append(html);            
        })
    })

	// 修改库存判断价格
    $(".stock").change(function(){//{{{
		var t = $(this);

		var num = $(this).val();
		var room_id = $(this).data("id");
		var product_id = $("#product_id").val();

		console.log(num);
		// 检查该库存修改后的数量是否小于已售卖的数量
        $.get("/product/product-room/check-sell2", {product_id:product_id, id:room_id, num:num}, function(data){
			if(data != 'true'){
				alert('修改后的舱房数量不能小于已售出的数量');	
				t.val(data);
			}
        });
	})//}}}
	


</script>
<?php app\components\JsBlock::end() ?>
